// pages/shop/shop.js
var app = getApp();
var that
Page({
  data: {
    success: 1,  //支付成功后返回首页
    orderid: '',  //支付成功的订单号
 
    productData: [],
    pages: 1,  //获取第几页的数据
    shuaxin: true,
    keyword: '',
    no_pro: false,  //是否搜索到商品
    
  },
  onLoad: function (options) {
    that = this;
    that.setData({ keyword: options.keyword, orderid: options.orderid, })
  },
  onReady: function () {

  },
  onShow: function () {
    that.setData({ pages: 1, productData: [], shuaxin: true, no_pro: false, });
    that.get_shop();
  },
 
  select_shop: function () {
    if (that.data.keyword != "") {
      that.setData({ pages: 1 })
      that.get_shop()
    } else { app.toast_up("无搜索内容!"); }
  },
  get_shop: function () {
    if (that.data.keyword != "" && that.data.pages == 1) {
      that.setData({ productData: [] })
    }
    app.req(app.local_url + "do=shop&p=goods_rank", { "keyword": that.data.keyword, "page": that.data.pages, "od": app.data.openid, }, function (res) {
      console.log(res)
      if (res.status == 1) {
        that.setData({ productData: res.result });return; //此为获取推荐商品的函数后续代码暂时无用

        if (that.data.productData.length == 0 && res.result.goods == '') {
          that.setData({ no_pro: true }); return;
        }
        if (res.result.goods == '') { app.toast_up("无更多数据"); return }//商品加载完了
        var p = that.data.productData.concat(res.result.goods)   //增加刷新的商品
        that.setData({ productData: p, pages: that.data.pages + 1 })

        setTimeout(function () { that.setData({ shuaxin: true }) }, 1000)
      } else if (res.status == 0) {
        app.toast_up(res.result)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_shop()
          }
        })
      }
    })
  },
  tobottom: function () {  //固定推荐十个暂时不需要分页
    // if (that.data.shuaxin) {  
    //   that.setData({ shuaxin: false })
    //   that.get_shop();
    // }
  },
  back_f: function () { 
    wx.switchTab({
      url: './shop',
    })
   }, //点击返回首页时隐藏
  select_f:function(){  //查看订单详情
    wx.redirectTo({
      url: '../new_order/shop_order_detail?orderid=' + that.data.orderid
    })
  }
})